/**
 *
 * Licensed Materials - Property of IBM 
 * 
 * OCO Source Materials 
 * 
 * (C) Copyright IBM Corp. 2011, 2012 All Rights Reserved 
 * 
 * The source code for this program is not published or other- 
 * wise divested of its trade secrets, irrespective of what has 
 * been deposited with the U.S. Copyright Office. 
 *
 */

/**
 * CHANGE HiSTORY:
 * Author             Time                 Description
 *
 */
package com.ibm.ba.demo.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ibm.ba.demo.entity.SPSSModelConfiguration;
import com.ibm.ba.demo.helper.BeanHelper;

@Service
public class InDBScoreRequestServiceImpl {

	private static Logger logger = Logger
			.getLogger(InDBScoreRequestServiceImpl.class);

	@Autowired
	private SPSSModelConfiguration modelConfig;

	public Map<String, Object> convert(
			Map<String, Object> paramMap) {
		logger.info("original paramMap: " + BeanHelper.beanToJson(paramMap));
		Map<String, Object> inputParamMap = createInitParamMap();
		for (Entry<String, Object> entry : paramMap.entrySet()) {
			if (entry.getKey().equalsIgnoreCase(modelConfig.getCustField())) {
				inputParamMap.put(entry.getKey(), entry.getValue());
			} else {
				inputParamMap.put(entry.getKey(), modelConfig.getSpssProdTrueValue());
			}
		}
		logger.info("after convertion: " + BeanHelper.beanToJson(inputParamMap));
		return inputParamMap;
	}

	public List<Map<String, Object>> convert(
			List<Map<String, Object>> paramMapList) {
		logger.info("original paramMapList: " + BeanHelper.beanToJson(paramMapList));
		List<Map<String, Object>> inputParamList = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> paramMap : paramMapList) {
			inputParamList.add(convert(paramMap));
		}
		logger.info("after convertion: " + BeanHelper.beanToJson(inputParamList));
		return inputParamList;
	}

	private Map<String, Object> createInitParamMap() {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		for (int i = 0; i < 50; i++) {
			paramMap.put(modelConfig.getSpssProductPrefix() + i,
					modelConfig.getSpssProdDefaultValue());
		}
		paramMap.put(modelConfig.getCustField(),
				modelConfig.getCustDefaultValue());
		return paramMap;
	}
}
